package com.efrei.uchat.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import com.efrei.uchat.client.gui.control.UChatControl;


public class Tools {

	/**
	 * join s with separator glue
	 * @param s values joined
	 * @param glue separator
	 * @return
	 */
	public static String combine(String[] s, String glue)
	{
		int k=s.length;
		if (k==0)
			return null;
		StringBuilder out=new StringBuilder();
		out.append(s[0]);
		for (int x=1;x<k;++x)
			out.append(glue).append(s[x]);
		return out.toString();
	}
	
	public final static String LOGGER_PATTERN = "%d{HH:mm:ss} - %C{2} [%t] - %-5p - %m%n";
	
	public static Logger initLog4jRootLogger() {
		Logger logger = Logger.getRootLogger();
		logger.setLevel(Level.DEBUG);
		logger.addAppender(new ConsoleAppender(new PatternLayout(LOGGER_PATTERN)));
		return logger;
	}
	
	/**
	 * @return executable (jar or bin/packages/class path)
	 * @throws UnsupportedEncodingException
	 */
	public static String getExecutablePath() throws UnsupportedEncodingException {
		String path = UChatControl.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		String decodedPath = URLDecoder.decode(path, "UTF-8");
		return new File(decodedPath).getParent();
	}
}
